//
//  ONFetcher.m
//  OrderNow
//
//  Created by Chickennohair on 2014/02/17.
//  Copyright (c) 2014年 Familly. All rights reserved.
//

#import "ONFetcher.h"

@implementation ONFetcher

+(NSArray*) arrayObjects:(Class) objClass fromArray:(NSArray*) arrDic;{
    NSMutableArray *arrayObjects = [[NSMutableArray alloc] init];
    for (NSDictionary *itemDic in arrDic) {
        NSError *err = nil;
        id mappingObject = [[objClass alloc] initWithDictionary:itemDic error:&err];
        if (err) {
            [arrayObjects removeAllObjects];
            return nil;
        }
        else{
            [arrayObjects addObject:mappingObject];
        }
    }
    return arrayObjects;
}

+(void) fetchJsonDataFromURL:(NSString*) url
                    method:(NSString*)method
                   jsonParam:(NSString*) param
                  completion:(void(^)(id jsonData, NSError *error)) callback{
    
    if ([method isEqualToString:@"GET"]) {
        if (param && param.length && ![param isEqualToString:@""]) {
            url = [NSString stringWithFormat:@"%@?%@",url,param];
        }
    }
    NSLog(@"%@",url);
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
    myFetcher.maxRetryInterval = 30;
    if ([method isEqualToString:@"GET"]) {
        if (param && param.length && ![param isEqualToString:@""]) {
            [request setHTTPMethod:[NSString stringWithFormat:@"%@",method]];
        }
    }
    else if ([method isEqualToString:@"POST"]){
        if (param && param.length && ![param isEqualToString:@""]) {
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPMethod:[NSString stringWithFormat:@"%@",method]];
            [request setHTTPBody: [param dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }

    //begin fetch data
    [myFetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
        callback(data,error);
    }];
}
+(void) fetchJsonDataFromFile:(NSString*) fileName
                    jsonParam:(NSString*) param
                   completion:(void(^)(id jsonData, NSError *error)) callback{
    NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@""] encoding:NSUTF8StringEncoding error:nil];
    NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    callback(jsonData,nil);
}


+(void)fetchObjects:(Class)objClass fromJSONData:(NSData *)data completion:(void (^)(NSArray *, NSError *))callback{
    NSArray *objects = [data objectFromJSONData];
    if (objects.count) {
        NSMutableArray *arrayObjects = [[NSMutableArray alloc] init];
        for (NSDictionary *itemDic in objects) {
            NSError *err = nil;
            id mappingObject = [[objClass alloc] initWithDictionary:itemDic error:&err];
            if (err) {
                callback([[NSMutableArray alloc] init],err);
                break;
            }
            else{
                [arrayObjects addObject:mappingObject];
            }
        }
        //return callback
        callback(arrayObjects,nil);
    }
    else{
        //empty
        callback([[NSMutableArray alloc] init],nil);
    }
}


+(void)fetchObjectsFromURL:(NSString *)url method:(NSString*)method jsonParam:(NSString *)json objectClass:(Class)objClass completion:(void (^)(NSArray *, NSError *))callback{
    
    if ([method isEqualToString:@"GET"]) {
        if (json && json.length && ![json isEqualToString:@""]) {
            url = [NSString stringWithFormat:@"%@?%@",url,json];
        }
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
    myFetcher.maxRetryInterval = 30;
    if (json && json.length && ![json isEqualToString:@""] && [method isEqualToString:@"POST"]) {
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPMethod:[NSString stringWithFormat:@"%@",method]];
        [request setHTTPBody: [json dataUsingEncoding:NSUTF8StringEncoding]];
    }
    //begin fetch data
    [myFetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
        [ONUtility handleErrorWithResponse:data];
        [self fetchObjects:objClass fromJSONData:data completion:^(NSArray *objects, NSError *error) {
            callback(objects,error);
        }];
    }];
}

+(void)fetchObjectsFromFile:(NSString *)fileName jsonParam:(NSString *)json objectClass:(Class)objClass completion:(void (^)(NSArray *, NSError *))callback{
    NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:@""] encoding:NSUTF8StringEncoding error:nil];
    NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    [self fetchObjects:objClass fromJSONData:jsonData completion:^(NSArray *objects, NSError *error) {
        callback(objects,error);
    }];
    
}


@end
